<?php

namespace App\Repositories\Log;


use App\Facade\AppContext;
use App\Models\Log\Import;
use App\Utils\Output;
use App\Utils\Pager;

/**
 * 导入日志
 * Class ImportLogRepository
 * @package App\Repositories
 */
class ImportRepository
{
    function adminPager($params)
    {
        extract($params);
        $query = Import::query();
        if (!empty($keyword)) {
            $query->where('log__imports.file_name', 'like', "%$keyword%");
        }

        if (!empty($from)) {
            $query->where('log__imports.import_time', '>=', strtotime($from));
        }

        if (!empty($to)) {
            $query->where('log__imports.import_time', '<=', strtotime($to . ' 23:59:59'));
        }

        if (!empty($type)) {
            $query->where('log__imports.type', '=', $type);
        }

        $total = $query->count();
        $query->leftJoin('system_users', 'system_users.id', '=', 'log__imports.user_id');
        $query->select(
            'log__imports.id',
            'log__imports.type',
            'log__imports.total',
            'log__imports.success',
            'log__imports.error',
            'log__imports.file_name',
            'system_users.name',
            'log__imports.import_time'
        );
        $lis = Pager::setPageParams($query, $params)->get();
        return Output::pager($lis, $total);
    }

    function get($id)
    {
        return Import::find($id);
    }

    function add($data)
    {
        $data['import_time'] = time();
        $data['user_id'] = AppContext::userId();
        Import::create($data);
    }

}
